package com.gitee.flwwsg.rpc.serialize.impl;

import com.gitee.flwwsg.rpc.serialize.Serializer;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * 字符串序列化, 使用 utf8 编码
 */
public class IntegerSerializer implements Serializer<Integer> {

    @Override
    public int size(Integer entry) {
        //int长度+int
        return Integer.BYTES + Integer.BYTES;
    }

    @Override
    public void serialize(Integer entry, byte[] bytes, int offset, int length) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes, offset, length);
        buffer.putInt(Integer.BYTES);
        buffer.putInt(entry);
    }

    @Override
    public Integer parse(byte[] bytes, int offset, int length) {
        ByteBuffer buffer = ByteBuffer.wrap(bytes, offset, length);
        buffer.getInt();
        return buffer.getInt();
    }

    @Override
    public byte type() {
        return Types.TYPE_INTEGER;
    }

    @Override
    public Class<Integer> getSerializeClass() {
        return Integer.class;
    }
}
